package com.example.a086.httpdemo;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.example.a086.httpdemo.http.Constant;
import com.example.a086.httpdemo.http.HttpCallBack;
import com.example.a086.httpdemo.http.HttpClientEngine;
import com.example.a086.httpdemo.http.base.HttpUtils;
import com.google.gson.Gson;



import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = findViewById(R.id.tv);
        HttpUtils.init(new HttpClientEngine());

        String body = new Gson().toJson(new SecurityCodeRequest("18770055726", System.currentTimeMillis() + "", 0));
        Log.e("asd", "验证码body=" + body);
        HttpUtils.with(MainActivity.this).url(Constant.BASE_URL + "apiwrite/system/verification/code/get")
                .body(body)
                .post().excute(new HttpCallBack<SecurityCodeResponse>() {
            @Override
            public void onSuccess(SecurityCodeResponse securityCodeResponse) {
                mTextView.setText("验证码success" + securityCodeResponse.getError_message() + securityCodeResponse.getError_code() + securityCodeResponse.getData().getCode());
            }

            @Override
            public void onFailed(String errorMsg) {
                mTextView.setText("验证码failed" + errorMsg);
            }
        });


        Map<String, Object> params = new HashMap<>();
        params.put("user_id", 5);
        params.put("timestamp", System.currentTimeMillis() + "");
        Map<String, File> files = new HashMap<>();
        files.put("file", getFiles().get(0));
        HttpUtils.with(this).url(Constant.BASE_URL+"apiwrite/user/avatar/upload")
                .addParams(params).addFiles(files)
                .excute(new HttpCallBack<SecurityCodeResponse>() {
                    @Override
                    public void onSuccess(SecurityCodeResponse securityCodeResponse) {
                        Log.e("asd","上传头像success"+securityCodeResponse.getError_message() + securityCodeResponse.getError_code());
                    }

                    @Override
                    public void onFailed(String errorMsg) {
                        Log.e("asd","上传头像failed"+errorMsg);
                    }
                });

    }


    public List<File> getFiles() {
        List<File> fileList = new ArrayList<>();
        String dir = Environment.getExternalStorageDirectory() + "/MyDownLoad/";
        File fileDir = new File(dir);
        File[] files = fileDir.listFiles();
        if (fileDir != null) {
            for (File file : files) {
                if (file.isFile()) {
                    fileList.add(file);
                }
            }
        }
        return fileList;
    }
}